/**
 * Copyright © 2015 eqxiu.com 北京中网易企秀科技有限公司  All rights reserved.
 */
package com.eqxiu.ieditor.api.controller.mall;

import com.eqxiu.ieditor.commons.web.MsgCode;
import eqxiu.mall.product.service.api.ProductServiceApi;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.eqxiu.ieditor.api.mall.IMallProductServiceApi;
import com.eqxiu.ieditor.commons.dto.IdDTO;
import com.eqxiu.ieditor.commons.web.ResultData;
import com.eqxiu.ieditor.dto.mall.ProductCodeTypeInputDTO;
import com.eqxiu.ieditor.dto.mall.ProductPageInputDTO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.Map;

/**
 * @author ranger
 * @description 商城-商品详情接口
 * @modified DuYoulong 重构
 * @date May 31, 2017
 */
@RestController
@Api(description = "商品Controller")
@CrossOrigin
public class ProductController {

    private final Logger LOGGER = LoggerFactory.getLogger(ProductController.class);


	@Autowired
	private ProductServiceApi productService;


    @Autowired
    private IMallProductServiceApi mallProductServiceApi;
	
	
	/*@GetMapping("/api/product/by-code/{code}")
	public ResultData info(@PathVariable("code") String code) {

		Map<String, Object> prod = productService.getProductLiteByCodeAndAttrGroupId(code, 2);// productService.getListBySourceId("")

		if (prod == null) {
			return fail().setCode(MsgCode.DATA_NOT_EXIST).setMsg("product not exist");
		}

		return success().setMap(prod);
	}*/

    @ApiOperation(value = "根据code查询商品的详情")
    @GetMapping("/api/product/by-code/{code}")
    @ResponseBody
    public ResultData getByTypeAndCode(@PathVariable("code") String code) {
        LOGGER.error("查询商品详情");
        ProductCodeTypeInputDTO dto = new ProductCodeTypeInputDTO();
        dto.setCode(code);
        dto.setAttrGroupId(2);
        return mallProductServiceApi.getByTypeAndCode(dto);
    }

	/*@PostMapping("/api/product/list")
	public ResultData list(@RequestParam Integer id, @RequestParam Integer pageNo, @RequestParam Integer pageSize,
			@RequestParam(required = false) Integer sort, @RequestParam(required = false) Integer priceRange) {
		Map<String, String> param = new HashMap<>();
		param.put("sort", extractSort(sort));
		param.put("priceRange", extractPriceRange(priceRange));

		PageDTO<Map<String, Object>> prods = productService.getProductByCategory(id, pageNo, pageSize, param);

		Map<String, Object> map = new HashMap<>();
		// map.put("count", prods.getCount());
		map.put("pageNo", pageNo);
		map.put("pageSize", pageSize);
		map.put("end", prods.isEnd());
		return success().setList(prods.getList()).setMap(map);
	}*/

    /*
     * 根据分类id查询商品的详情列表
     */
    @ApiOperation(value = "根据分类id查询商品的详情列表")
    @PostMapping("/api/product/list")
    @ResponseBody
    public ResultData pageList(@RequestParam Integer id, @RequestParam Integer pageNo, @RequestParam Integer pageSize,
                               @RequestParam(required = false) Integer sort, @RequestParam(required = false) Integer priceRange) {
        ProductPageInputDTO dto = new ProductPageInputDTO();
        dto.setCategoryId(id);
        dto.setPriceRange(priceRange);
        dto.setSort(sort);
        dto.setPageNo(pageNo);
        dto.setPageSize(pageSize);

        return mallProductServiceApi.getPageListByCategoryId(dto);
    }

	/*@GetMapping("/api/act/{actId}")
	public ResultData activityList(@PathVariable Integer actId) {
		List<Map<String, Object>> products;
		try {
			products = specialService.specialDetails(actId);
			return success().setList(products);
		} catch (ApiException e) {
			e.printStackTrace();
			return fail();
		}
	}*/

    /**
     * @return ResultData
     * @description 根据专题id（specialId）查询所属专题下的商品信息
     */
    @ApiOperation(value = "根据专题id（specialId）获取商品")
    @GetMapping("/api/act/{actId}")
    @ResponseBody
    public ResultData activityList(@PathVariable Integer actId) {
        IdDTO dto = new IdDTO();
        dto.setId(actId);
        return mallProductServiceApi.getListBySpecialId(dto);
    }

    /**
     * @return com.eqxiu.ieditor.commons.web.ResultData
     * @Description 根据商品id查询商品的详情
     * @Author SuiXiangyu
     * @Date 2019/4/11
     * @Param [productId]
     **/
    @ApiOperation(value = "根据商品id查询商品的详情")
    @PostMapping("/api/product/by-productid")
    @ResponseBody
    public ResultData getProductByProductId(Integer productId) {
        if (!"".equals(productId) && productId != null) {
            try {
                return mallProductServiceApi.getByProductId(productId);
            } catch (Exception e) {
                LOGGER.error(e.getMessage());
            }
        }
        return null;
    }


    @ApiOperation(value = "根据id查询商品的详情")
    @GetMapping("/api/product/by-id/{productId}")
    @ResponseBody
    public ResultData getByProductId(@PathVariable("productId") Integer productId) {
        LOGGER.error("查询商品详情");
        ProductCodeTypeInputDTO dto = new ProductCodeTypeInputDTO();
        Map<String, Object> prod = productService.getProductById(productId);
        if (prod == null) {
            return ResultData.result(false).setCode(MsgCode.DATA_NOT_EXIST).setMsg("product not exist");
        }
        return ResultData.result(true).setMap(prod);
    }
}
